package com.ejia.ui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.ejia.pojo.house;
import com.ejia.pojo.user;
import com.ejia.server.HouseService;
import com.ejia.server.UserService;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.awt.event.ActionEvent;
import java.awt.Toolkit;

public class YZXXGLBJ extends JFrame {

	private JPanel contentPane;
	private JTextField txtname;
	private JTextField txtpwd;
	private JTextField txtphone;
	private JTextField txtmph;
	private static ArrayList<house> houses;
	private static ArrayList<user> users;
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					YZXXGLBJ frame = new YZXXGLBJ();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public YZXXGLBJ() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(YZXXGLBJ.class.getResource("/com/ejia/photo/客户管理系统.png")));
		setTitle("业主信息管理编辑");
		
		users=UserService.findYZXXGLBJ();
		
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 645, 868);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("编辑信息");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setFont(new Font("宋体", Font.PLAIN, 42));
		label.setBounds(202, 33, 218, 65);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("业主姓名");
		label_1.setFont(new Font("宋体", Font.PLAIN, 32));
		label_1.setBounds(74, 113, 158, 47);
		contentPane.add(label_1);
		
		JLabel label_2 = new JLabel("业主账号:     "+users.get(YZXXGL.index).getUid());
		label_2.setFont(new Font("宋体", Font.PLAIN, 32));
		label_2.setBounds(74, 200, 473, 47);
		contentPane.add(label_2);
		
		JLabel label_3 = new JLabel("密码");
		label_3.setFont(new Font("宋体", Font.PLAIN, 32));
		label_3.setBounds(74, 287, 158, 47);
		contentPane.add(label_3);
		
		JLabel label_4 = new JLabel("性别");
		label_4.setFont(new Font("宋体", Font.PLAIN, 32));
		label_4.setBounds(74, 374, 158, 47);
		contentPane.add(label_4);
		
		JLabel label_5 = new JLabel("电话");
		label_5.setFont(new Font("宋体", Font.PLAIN, 32));
		label_5.setBounds(74, 461, 158, 47);
		contentPane.add(label_5);
		
		JLabel label_6 = new JLabel("小区");
		label_6.setFont(new Font("宋体", Font.PLAIN, 32));
		label_6.setBounds(74, 548, 158, 47);
		contentPane.add(label_6);
		
		JLabel label_7 = new JLabel("门牌号");
		label_7.setFont(new Font("宋体", Font.PLAIN, 32));
		label_7.setBounds(74, 635, 158, 47);
		contentPane.add(label_7);
		
		
		
		txtname = new JTextField();
		txtname.setText(users.get(YZXXGL.index).getRealname());
		txtname.setFont(new Font("宋体", Font.PLAIN, 24));
		txtname.setBounds(306, 123, 241, 27);
		contentPane.add(txtname);
		txtname.setColumns(10);
		
		txtpwd = new JTextField();
		txtpwd.setText(users.get(YZXXGL.index).getPwd());
		txtpwd.setFont(new Font("宋体", Font.PLAIN, 24));
		txtpwd.setColumns(10);
		txtpwd.setBounds(306, 297, 241, 27);
		contentPane.add(txtpwd);
		
		txtphone = new JTextField();
		txtphone.setText(users.get(YZXXGL.index).getPhone());
		txtphone.setFont(new Font("宋体", Font.PLAIN, 24));
		txtphone.setColumns(10);
		txtphone.setBounds(306, 471, 241, 27);
		contentPane.add(txtphone);
		
		txtmph = new JTextField();
		txtmph.setText(users.get(YZXXGL.index).getHid().getRemark());
		txtmph.setFont(new Font("宋体", Font.PLAIN, 24));
		txtmph.setColumns(10);
		txtmph.setBounds(306, 645, 241, 27);
		contentPane.add(txtmph);
		
		JComboBox comboBox = new JComboBox();
		comboBox.setFont(new Font("宋体", Font.PLAIN, 24));
		String[] sex = {"男", "女"};
		comboBox.setModel(new DefaultComboBoxModel(sex));
		comboBox.setBounds(306, 384, 241, 27);
		contentPane.add(comboBox);
		
		JComboBox comboBox_1 = new JComboBox();
		comboBox_1.setFont(new Font("宋体", Font.PLAIN, 24));
		String[] items = {"丽景豪庭", "时代名都", "黄金国际", "中瑞华侨城"};
		comboBox_1.setModel(new DefaultComboBoxModel(items));
		comboBox_1.setBounds(306, 558, 241, 27);
		for (int i = 0; i < items.length; i++) {
			if (items[i].equals(users.get(YZXXGL.index).getHid().getHname())) {
				comboBox_1.setSelectedIndex(i);
			}
		}
		contentPane.add(comboBox_1);
		
		JButton button = new JButton("修改");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String name=txtname.getText();
				String pwd=txtpwd.getText();
				String phone=txtphone.getText();
				String mph=txtmph.getText();
				int parentid=0;
				if (comboBox_1.getSelectedItem().equals("丽景豪庭")) {
					parentid=1;
				}else if (comboBox_1.getSelectedItem().equals("时代名都")) {
					parentid=2;
				}else if (comboBox_1.getSelectedItem().equals("黄金国际")) {
					parentid=3;
				}else if (comboBox_1.getSelectedItem().equals("中瑞华侨城")) {
					parentid=4;
				}
				if (HouseService.yzxxadd(mph, parentid)) {
					houses=HouseService.find(mph);
					int hid=houses.get(0).getHid();
					if (UserService.yzxxglxg(users.get(YZXXGL.index).getUid(), pwd, name,(String) comboBox.getSelectedItem(), phone, hid)) {
						JOptionPane.showMessageDialog(null, "修改成功");
						new YZXXGL().setVisible(true);
						dispose();
					}else {
						JOptionPane.showMessageDialog(null, "修改失败");
					}
				}else {
					JOptionPane.showMessageDialog(null, "房号输入错误");
				}
			}
		});
		button.setFont(new Font("宋体", Font.PLAIN, 32));
		button.setBounds(125, 714, 123, 70);
		contentPane.add(button);
		
		JButton button_1 = new JButton("取消");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new YZXXGL().setVisible(true);
				dispose();
			}
		});
		button_1.setFont(new Font("宋体", Font.PLAIN, 32));
		button_1.setBounds(373, 714, 123, 70);
		contentPane.add(button_1);
		setLocationRelativeTo(null);
	}
}
